package com.lzx.iteam;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;
import com.lzx.iteam.bean.WeeklyMsgBean;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.GetWeeklyCommentMsg;
import com.lzx.iteam.net.GetWeeklyTemplateMsg;
import com.lzx.iteam.net.LocalLogin;
import com.lzx.iteam.provider.CloudDBOperation;
import com.lzx.iteam.util.DateUtil;
import com.lzx.iteam.util.ImageLoaderInterface;
import com.lzx.iteam.util.PreferenceUtil;
import com.lzx.iteam.widget.RoundImageView;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 05月 26日
 * Time: 下午3:50
 *
 * @QQ : 1234567890
 * **********************************
 */
public class WeeklyMsgActivity extends MyBaseActivity implements AdapterView.OnItemClickListener{

    @FindViewById(id = R.id.title_ll_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.ll_message_no_data)
    private LinearLayout mLayoutNoData;
    @FindViewById(id = R.id.title_tv_center)
    private TextView mTvCenter;
    @FindViewById(id = R.id.weekly_msg_lv)
    private ListView mListView;

    private String mGroupId;
    private String mUserId;

    private ArrayList<WeeklyMsgBean> mDatas =new ArrayList<WeeklyMsgBean>();
    private WeeklyMsgdapter mAdapter;
    private CloudDBOperation mCloudDBOperation;

    private static final int WEEKLY_MSG_LIST = 10001;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case WEEKLY_MSG_LIST:
                    if (msg.arg1 == 0){
                        ArrayList<WeeklyMsgBean> newDatas = (ArrayList<WeeklyMsgBean>) msg.obj;
                        if (newDatas.size() > 0){
//                            mDatas.addAll(newDatas);
//                            mCloudDBOperation.deleteAllWeeklyUnreadList(mGroupId,mUserId);
                            mCloudDBOperation.insertWeeklyUnreadListData(mUserId,newDatas);
                            getLocalMsgList();
                        }

                    }else{
                        if (msg.arg1 == AsynHttpClient.ERROR_LOCAL) {
                            Toast.makeText(mActivity, R.string.active_failure_no_network,
                                    Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(mActivity, (String) msg.obj,
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                    break;
            }
        }
    };

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState,R.layout.weekly_message_layout);
    }

    @Override
    public void initListener() {
        mLayoutBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mListView.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        mGroupId = getIntent().getStringExtra("groupId");
        mTvCenter.setText("我的消息");
        mCloudDBOperation = new CloudDBOperation(this);
        mUserId = mPreferenceUtil.getString(PreferenceUtil.CLIENT_USEID, "－1");

        mAdapter = new WeeklyMsgdapter();
        mListView.setAdapter(mAdapter);

    }

    @Override
    protected void onResume() {
        getLocalMsgList();
        getUnReadMsgList();
        super.onResume();
    }

    private void getLocalMsgList(){
        mDatas = mCloudDBOperation.findWeeklyUnreadListData(mUserId,mGroupId);
        mAdapter.notifyDataSetChanged();
        if (mDatas.size() == 0){
            mLayoutNoData.setVisibility(View.VISIBLE);
        }else{
            mLayoutNoData.setVisibility(View.GONE);
        }
    }


    /**
     * 获取模板
     */
    private void getUnReadMsgList() {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
                .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_GROUP_ID, mGroupId));
        GetWeeklyCommentMsg message = new GetWeeklyCommentMsg(mHandler.obtainMessage(WEEKLY_MSG_LIST));
        message.mApi = AsynHttpClient.API_EVENT_WEEKLY_UNREAD_MSG;
        message.mParams = nvps;
        AsynHttpClient.getInstance(this).execEventHttp(message);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent detail = new Intent(mActivity,WeeklyDetailActivity.class);
        detail.putExtra("groupId",mGroupId);
        detail.putExtra("weeklyId",mDatas.get(position).getWeekly_id());
        mCloudDBOperation.deleteOneWeeklyUnreadList(mGroupId,mDatas.get(position).getWeekly_id(),mDatas.get(position).getComment_time(),mUserId);
        startActivity(detail);

    }

    @Override
    public void onClick(View v) {

    }


    class WeeklyMsgdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return mDatas == null?0:mDatas.size();
        }

        @Override
        public Object getItem(int position) {
            return mDatas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.weekly_msg_item_layout,null);
                holder.userHead = (RoundImageView) convertView.findViewById(R.id.talk_message_item_user_img);
                holder.userName = (TextView) convertView.findViewById(R.id.talk_message_item_user_name);
                holder.commentTime = (TextView) convertView.findViewById(R.id.talk_message_item_user_time);
                holder.commentContent = (TextView) convertView.findViewById(R.id.talk_message_item_comment);
                holder.weekDate = (TextView) convertView.findViewById(R.id.msg_tv_date);
                holder.weekContent = (TextView) convertView.findViewById(R.id.msg_tv_content);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            WeeklyMsgBean weeklyMsgBean = mDatas.get(position);
            ImageLoaderInterface.imageLoader.displayImage(weeklyMsgBean.getComment_user_image(),holder.userHead,ImageLoaderInterface.optionHeadImage);
            holder.userName.setText(weeklyMsgBean.getComment_user_name());
            holder.commentTime.setText(DateUtil.getChatTime(Long.parseLong(weeklyMsgBean.getComment_time()) * 1000));
            holder.commentContent.setText(weeklyMsgBean.getComment_content());
            holder.weekDate.setText(weeklyMsgBean.getWeekly_title());
//            holder.weekContent.setText(weeklyMsgBean.getWeekly_content());
            return convertView;
        }
    }

    class ViewHolder{
        private RoundImageView userHead;
        private TextView userName;
        private TextView commentTime;
        private TextView commentContent;
        private TextView weekDate;
        private TextView weekContent;
    }

}
